package cn.hu.org.service.impl;

import cn.hu.basic.commonenum.ErrorCode;
import cn.hu.basic.constants.Constant;
import cn.hu.basic.domain.vo.ShopStatisticsVo;
import cn.hu.basic.exception.CustomException;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.BaiduAuditUtils;
import cn.hu.org.domain.Employee;
import cn.hu.org.domain.Shop;
import cn.hu.org.domain.ShopAuditLog;
import cn.hu.org.mapper.EmployeeMapper;
import cn.hu.org.mapper.ShopAuditLogMapper;
import cn.hu.org.mapper.ShopMapper;
import cn.hu.org.service.IShopService;
import cn.hu.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hu
 * @since 2022-12-17
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Override
    public void settle(Shop shop) {
        //参数校验 spring validation
        //两次密码是否一致
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new CustomException(ErrorCode.PASSWORD_CHECK_FAILED.getMessage());
        }
        //校验店铺是否注册过
        Shop shopTmp = shopMapper.findByName(shop.getName());
        if(shopTmp != null){
            throw new CustomException(ErrorCode.SHOP_ALREADY_EXIST.getMessage());
        }
        Boolean auditFlag = false;
        //百度Ai文本审核
        auditFlag = BaiduAuditUtils.TextCensor(shop.getName());
        if (!auditFlag){
            throw new CustomException(ErrorCode.CONTENT_AUDIT_FAILED.getMessage());
        }
        //图片审核
        if(shop.getLogo() != null){
            auditFlag =  BaiduAuditUtils.ImgCensor(Constant.IMG_SHOW_URL+shop.getLogo());
            //如果出现不合规内容，则抛出业务异常
            if(!auditFlag){
                throw new CustomException(ErrorCode.LOGO_AUDIT_FAILED.getMessage());
            }
        }


        //初始化店铺状态
        shop.setState(0);
        //店铺初始化注册时间
        shop.setRegisterTime(new Date());
        //保存店铺
        shopMapper.add(shop);
        //店长是否注册过
        Employee emp = employeeMapper.findByUserName(shop.getAdmin().getUsername());
        //店长如果注册过是否是别的店铺的
        if(emp != null && emp.getShopId() != null){
            throw new CustomException(ErrorCode.SHOPADMIN_ALREADYEXSIT.getMessage());
        }
        //如果店长存在但是没有shopid就直接绑定本次shop的id
        if(emp != null){
            //员工有，并且可以成为这次的店长
            emp.setShopId(shop.getId());
            shop.setAdminId(emp.getId());
        }else{
            //员工绑定店铺id
            emp = shop.getAdmin();
            emp.setShopId(shop.getId());
            //给新增的员工添加状态
            emp.setState(0);
            //添加员工
            employeeMapper.add(emp);
            //给店铺绑定店长
            shop.setAdminId(shop.getAdmin().getId());
        }
        //更新店铺
        shopMapper.update(shop);
    }

    @Override
    public void pass(ShopAuditLog log) {
        //修改店铺和(店长的状态应该再激活店铺的时候改为正常状态)
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setAdmin(employeeMapper.findById(shop.getAdminId()));
        shop.setState(2);//修改店铺为待激活状态
        shopMapper.update(shop);
        //设置审核日志状态 1通过0驳回
        log.setState(1);
        //设置审核人（以后做成动态的）
        log.setAuditId(334L);
        //设置审核时间
        log.setAuditTime(new Date());
        shopAuditLogMapper.add(log);
    }

    @Override
    public void reject(ShopAuditLog log) {
        //修改店铺和(店长的状态应该再激活店铺的时候改为正常状态)
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setAdmin(employeeMapper.findById(shop.getAdminId()));
        shop.setState(-1);//修改店铺为驳回状态
        shopMapper.update(shop);
        //设置审核日志状态 1通过0驳回
        log.setState(0);
        //设置审核人（以后做成动态的）
        log.setAuditId(334L);
        //设置审核时间
        log.setAuditTime(new Date());
        shopAuditLogMapper.add(log);
    }

    @Override
    public AjaxResult statistics() {
        //查询统计的数据
        List<ShopStatisticsVo> statistics = shopMapper.statistics();
        List<String> x  = new ArrayList<>();
        List<Integer> xValue = new ArrayList<>();
        //把x轴的值装在一个集合，把xValue装在一个集合
        for (ShopStatisticsVo statistic : statistics) {
            Integer state = statistic.getState();
            switch (state){
                case -1:x.add("驳回");break;
                case 0:x.add("待审核");break;
                case 1:x.add("审核通过");break;
                case 2:x.add("待激活");break;
            }
            xValue.add(statistic.getCountNum());
        }
        //封装HashMap表示x轴的集合以及x对应值的集合
        HashMap hm = new HashMap<String,List<Integer>>();
        hm.put("x",x);
        hm.put("xValue",xValue);
        return AjaxResult.getInstance().setResultObject(hm);
    }
}
